import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)


const userAbout = {
	namespaced: true,
	state: {
		userInfo: {
			isLogin: false,
			// id: 1949705591,
			id:"",
			avatarUrl: require("../assets/ava.jpg"),
			// avatarUrl: "https://p4.music.126.net/Fg2EAac7WFvx9a8hh_9_pA==/109951166475365714.jpg",
			nickname: ""
		}
	},
	actions: {},
	mutations: {
		UPDATEUSERINFO(state, value) {
			state.userInfo = value
		}
	},
}

const songAbout = {
	namespaced: true,
	state: {
		playing: false,      //播放状态
		singinglist: [],     //歌曲列表信息
		index: 0,
		playUrl: "",         //当前歌曲url
		songInfo: {
			id: "",
			al: {
				picUrl: ""   //歌曲封面图片
			},
			name: "",        //歌曲名字
			ar: [
				{
					name: ""  //作者名字
				}
			]

		}
	},
	actions: {
		playsongid(context, value) {

			const id = context.state.singinglist[value].id
			
			axios.get("http://localhost:8013/api/song/url?id=" + id).then(
				res => {
					console.log(res);
					context.commit("INDEX", value)
					console.log(res.data.data[0].url);
					context.commit("PLAY", res.data.data[0].url)

				}
			)

			axios.get("http://localhost:8013/api/song/detail?ids=" + id).then(
				res => {
					context.commit("SONG", res.data.songs[0])
					//本地存储一下,播放记录
					let name = res.data.songs[0].name
					let ar = res.data.songs[0].ar[0].name
					const myDate = new Date();
					const mytime = myDate.toLocaleTimeString();     //获取当前时间

					if (localStorage.getItem("singHistory")) {
						//如果本地有播放记录，取到历史数据
						const history = JSON.parse(localStorage.getItem("singHistory"))
						//遍历判断本次新数据是否与历史数据重复
						let result = history.findIndex((item) => {
							return item.id === id;
						});
						if (result !== -1) {
							//历史记录中存在该首歌曲
							history.splice(result, 1);
						}
						//生成本次新数据对象
						let data = { id: id, time: mytime, name: name, ar: ar }
						//将本次数据加入历史数据
						history.unshift(data)
						//重新本地存储
						localStorage.setItem("singHistory", JSON.stringify(history))

					}
					else {
						//本地没有播放记录，生成本次新数据，首次存储为本地数据
						let data = [{ id: context.state.singinglist[value].id, time: mytime, name: name, ar: ar }]
						localStorage.setItem("singHistory", JSON.stringify(data))
					}
				}
			)
		},
		replaceOneByOne(context,value){
			console.log(context.state.singinglist);
			const num = context.state.singinglist.length
			value.forEach(element => {
				axios.get("http://localhost:8013/api/song/detail?ids="+element.id).then(
					res=>{
						context.state.singinglist.push(res.data.songs[0])
					}
				)
			});
			console.log(context.state.singinglist);
			// context.state.singinglist.splice(0,num)
			console.log(context.state.singinglist);


			// const newList = []
			// value.forEach(element => {
			// 	axios.get("http://localhost:8013/api/song/detail?ids="+element.id).then(
			// 		res=>{
			// 			newList.push(res.data.songs[0])
			// 		}
			// 	)
			// });
			// console.log(newList);
			// context.commit("UPDATESONG",newList)
		},
		mountTheFirst(context,value){
			axios.get("http://localhost:8013/api/song/url?id=" + value).then(
				res=>{
					console.log(res);
					context.commit("INDEX", value)
					context.commit("PLAY", res.data.data[0].url)
				}
			)
			axios.get("http://localhost:8013/api/song/detail?ids=" + value).then(
				res=>{
					context.commit("SONG", res.data.songs[0])
				}
			)
		}
	},
	mutations: {
		//点击歌单将歌曲添加到播放列表
		UPDATESONG(state, value) {
			state.singinglist = value
			state.index = 0
		},
		//获取当前歌曲的播放url
		PLAY(state, value) {
			console.log(value);
			state.playUrl = value
		},
		//获取当前播放歌曲的详细信息
		SONG(state, value) {
			state.songInfo = value
		},
		//改变播放状态  播放或暂停
		CHANGE(state, value) {
			state.playing = value
		},
		INDEX(state, value) {
			state.index = value
		},
		//最近播放页面和每日推荐逐个全部替换
		
	},
}

export default new Vuex.Store({
	modules: {
		userAbout,
		songAbout
	}
})

